package com.example.cms.Service.Impl;

import com.example.common.Utils.PageUtil;
import com.example.cms.Service.IConfigService;
import com.example.user.bean.Config;
import com.example.user.dao.ConfigMapper;
import com.example.user.dao.Extend.ConfigExtendMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.protobuf.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/31-10-31-14:23
 * @Description：com.example.cms.Service.Impl
 */
@Service
public class ConfigServiceImpl implements IConfigService {
    @Autowired
    private ConfigMapper mapper;
    @Autowired
    private ConfigExtendMapper extendMapper;
    @Override
    public List<Config> selectAllConfig() {
        return extendMapper.selectAllConfig();
    }

    //添加配置
    @Override
    public void addConfig(Config config) {
        //名字不能重复
        if(extendMapper.selectByName(config.getConfigName())!=null){
            throw new RuntimeException("配置用户名已存在");
        }
        //添加
        mapper.insert(config);
    }

    //更新配置信息
    @Override
    public void updateConfig(Config config) {
        if(mapper.selectByPrimaryKey(config.getConfigId()) == null){
            throw new RuntimeException("您需要更新的用户不存在");
        }
        mapper.updateByPrimaryKeySelective(config);
    }

    //更新配置状态
    @Override
    public void updateStatusById(Integer id) {
        Config config = mapper.selectByPrimaryKey(id);
        if(config == null){
            throw new RuntimeException("配置信息不存在");
        }
        if(config.getConfigStatus() == 0){
            config.setConfigStatus(1);
        }else{
            config.setConfigStatus(0);
        }
        extendMapper.updateStatusById(config.getConfigId(),config.getConfigStatus());
    }

    //删除配置信息
    @Override
    public void deleteConfigById(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    //分页查找配置信息  pages 页数  nums 每页展示的条数
    @Override
    public PageInfo<Config> selectConfigPages(Integer pageNum, Integer pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Config> list =  extendMapper.selectConfigs();
        System.out.println(list);
        //PageInfo<Config> info = new PageInfo<>(list);
        return new PageInfo<>(list);
        //return null;
    }

    @Override
    public PageUtil selectListByPage(PageUtil pageUtil) {
        //插件 构建limit条件
        PageHelper.startPage(pageUtil.getPageNumber(),pageUtil.getPageSize());
        //带条件查询 类似于Pageinfo
        Page<Config> configs = extendMapper.selectConfigs(null);
        pageUtil.setList(configs.getResult());
        pageUtil.setTotal(configs.getTotal());
        return pageUtil;
    }
}
